/*
 * Copyright (C), 2002-2015, 苏宁易购电子商务有限公司
 * FileName: CheckLoginFilter.java
 * Author:   admin
 * Date:     2015年12月31日 上午9:50:29
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.app.snpaasadmin.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class CheckLoginFilter implements Filter {

    private static final String USERNAME = "username";
    FilterConfig filterConfig = null;
    private String redirectURL = null;
    private static HttpSession session = null;

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
        this.filterConfig = null;
        if(StringUtil.isNotEmpty(USERNAME) && StringUtil.isNotNullString(USERNAME)) {
            session.removeAttribute(USERNAME);
        }
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
     * javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        session = request.getSession();
        String requestType = request.getHeader("X-Requested-With");
        
        if(session.getAttribute(CheckLoginFilter.USERNAME) == null
                && (request.getRequestURI().startsWith("/index.html")||request.getRequestURI().equals(request.getContextPath()))) {
            response.sendRedirect(request.getContextPath() + redirectURL);
            return;
        }
        
        if (checkResource(request)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        
        if(session.getAttribute(CheckLoginFilter.USERNAME) == null
                && requestType != null && requestType.equals("XMLHttpRequest")) {
            response.sendError(401);
            return;
        }
        
        if(session.getAttribute(CheckLoginFilter.USERNAME) == null) {
            response.sendRedirect(request.getContextPath() + redirectURL);
            return;
        }
        
        
        
        filterChain.doFilter(servletRequest, servletResponse);

    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.redirectURL = filterConfig.getInitParameter("redirectURL");
    }
    
    private boolean checkResource(HttpServletRequest request) {
        if (request.getRequestURI().toLowerCase().endsWith(".js"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".css"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".png"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".jpg"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".html"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".ttf"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".svg"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".eot"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".woff"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".jpeg"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".gif"))
            return true;
        else if (request.getRequestURI().toLowerCase().endsWith(".ico"))
            return true;
        else if (request.getRequestURI().startsWith("../fonts"))
            return true;
        else 
            return false;
    }

}
